package com.yyn.edumsm.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.google.gson.Gson;
import com.yyn.edumsm.service.MsmService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.CompletableFuture;

@Service
public class MsmServiceImpl implements MsmService {
    @Override
    public boolean send(Map<Object, Object> param, String phone) {
//        if(StringUtils.isEmpty(phone)) return false;
//
//        DefaultProfile profile = DefaultProfile.getProfile("default", "LTAI5tLzaWP2HmZBpVdpvwcE", "qYhW7dEuyP0deegLJ1bQbt9hzkigml");
//        IAcsClient client = new DefaultAcsClient(profile);
//
//        // 1.发送短信
//        SendSmsRequest request = new SendSmsRequest();
//        request.setPhoneNumbers(phone);//接收短信的手机号码
//        request.setSignName("阿里云短信测试");//短信签名名称
//        request.setTemplateCode("SMS_154950909");//短信模板CODE
//        request.setTemplateParam(code);//短信模板变量对应的实际值
//
//        try {
//            SendSmsResponse response = client.getAcsResponse(request);
//
//            System.out.println(new Gson().toJson(response));
//        } catch (ServerException e) {
//            e.printStackTrace();
//        } catch (ClientException e) {
//            System.out.println("ErrCode:" + e.getErrCode());
//            System.out.println("ErrMsg:" + e.getErrMsg());
//            System.out.println("RequestId:" + e.getRequestId());
//        }
        if(StringUtils.isEmpty(phone)) return false;
        DefaultProfile profile = DefaultProfile.getProfile("default", "LTAI5tLzaWP2HmZBpVdpvwcE", "qYhW7dEuyP0deegLJ1bQbt9hzkigml");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");

        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "阿里云短信测试");//签名
        request.putQueryParameter("TemplateCode","SMS_154950909");//模板id
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));

        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response.getHttpResponse().isSuccess();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }

}
